package com.tvunetworks.center.user.controller;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.UpdateInstanceParam;
import com.tvunetworks.center.user.model.UserInstance;
import com.tvunetworks.center.user.model.param.InstanceIdWithPeerId;
import com.tvunetworks.center.user.model.param.InstanceParam;
import com.tvunetworks.center.user.model.param.PageUserInstanceParam;
import com.tvunetworks.center.user.model.vo.UserInstanceVo;
import com.tvunetworks.center.user.service.InstancePeerIdService;
import com.tvunetworks.center.user.service.UserInstanceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @program: tvucc-aggregator
 * @description: UserInstanceController
 * @author lebronchen
 * @create: 2020-04-23 15:03
 **/
@RestController
@RequestMapping("/userInstance")
public class UserInstanceController {

    @Autowired
    private UserInstanceService userInstanceService;
    @Autowired
    private InstancePeerIdService instancePeerIdService;

    /**
     *
     * @param loginUser loginUser
     * @param instanceParam  instanceParam
     * @return Result
     * @throws IOException 编译异常
     */
    @PostMapping("/listActiveRegion")
    public Result listActiveRegion(@Logined LoginUser loginUser, @RequestBody InstanceParam instanceParam) throws IOException {
        String imageType = null;
        if (instanceParam != null) {
            imageType = instanceParam.getImageType();
        }
        List list = userInstanceService.listActiveRegion(loginUser, imageType);
        return ResultUtil.success(list);
    }

    /**
     *
     * @param session session
     * @param loginUser  loginUser
     * @param instanceParam instanceParam
     * @return Result
     * @throws IOException 编译异常
     */
    @PostMapping("/createInstance")
    public Result createInstance(@RequestHeader(value = "authorization") String session, @Logined LoginUser loginUser, @RequestBody InstanceParam instanceParam) throws IOException {
        if (StringUtils.isEmpty(instanceParam.getRegion())) {
            return ResultUtil.paramError();
        }
        UserInstance userInstance = userInstanceService.createInstance(session, loginUser, instanceParam);
        return ResultUtil.success(userInstance);
    }

    /**
     *
     * @param loginUser loginUser
     * @param instanceParam instanceParam
     * @return Result
     * @throws IOException  编译异常
     */
    @PostMapping("/shutdownInstance")
    public Result shutdownInstance(@Logined LoginUser loginUser, @RequestBody InstanceParam instanceParam) throws IOException {
        if (StringUtils.isEmpty(instanceParam.getId())) {
            return ResultUtil.paramError();
        }
        userInstanceService.shutdownInstance(loginUser, instanceParam);
        return ResultUtil.success();
    }

    /**
     *
     * @param param param
     * @return Result
     * @throws IOException 编译异常
     */
    @PostMapping("/updateInstance")
    public Result updateInstance(@RequestBody @Valid UpdateInstanceParam param) throws IOException {
        userInstanceService.updateInstance(param);
        return ResultUtil.success();
    }

    /**
     *
     * @param param param
     * @return Result
     */
    @PostMapping("/page")
    public Result pageUserInstance(@RequestBody PageUserInstanceParam param) {
        PageInfo<UserInstanceVo> pageInfo = userInstanceService.pageUserInstance(param);
        return ResultUtil.success(pageInfo);
    }

    /**
     *
     * @return Result
     * @throws IOException 编译异常
     */
    @PostMapping("/multiRNum")
    public Result multiRNum() throws IOException {
        Map<String, String> map = userInstanceService.multiRNum();
        return ResultUtil.success(map);
    }

    /**
     *
     * @return Result
     * @throws IOException 编译异常
     */
    @PostMapping("/multiImageType")
    public Result multiImageType() throws IOException {
        Map<String, String> map = userInstanceService.multiImageType();
        return ResultUtil.success(map);
    }

    /**
     *
     * @param param param
     * @return Result
     */
    @PostMapping("/getWebRUrl")
    public Result getWebRUrl(@RequestBody @Valid InstanceIdWithPeerId param){
        String url = instancePeerIdService.getWebRUrl(param);
        return ResultUtil.success(url);
    }

    /**
     *
     * @return Result
     */
    @GetMapping("/sendNotShutdownEmailToSupport")
    public Result sendNotShutdownEmailToSupport(){
        userInstanceService.sendRunningInstanceEmailToSupport();
        return ResultUtil.success();
    }


}
